package com.egao.common.module.notice.controller;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.notice.entity.Notice;
import com.egao.common.module.notice.service.NoticeService;
import com.egao.common.module.notice.utils.NoticeParams;
import com.egao.common.module.system.constants.DictConstants;
import com.egao.common.module.system.service.CollegeService;
import com.egao.common.module.system.service.DictService;
import com.egao.common.module.system.service.YearService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 企业号通知
 * @author root
 */
@Api(tags = "企业号通知")
@RestController
@RequestMapping("${api}")
public class NoticeController extends BaseController {

    @Autowired
    private YearService yearService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private DictService dictService;
    @Autowired
    private NoticeService noticeService;

    @PreAuthorize("hasAuthority('notice')")
    @ApiOperation("查询记录")
    @GetMapping("/notice/record")
    public PageResult<Notice> page(HttpServletRequest request) {
        PageParam<Notice> pageParam = new PageParam<>(request);
        return new PageResult<>(noticeService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('notice')")
    @ApiOperation("查询毕业年份")
    @GetMapping("/notice/year")
    public JsonResult year() {
        return yearService.listAll();
    }

    @PreAuthorize("hasAuthority('notice')")
    @ApiOperation("查询学院")
    @GetMapping("/notice/college")
    public JsonResult college() {
        return JsonResult.ok().setData(collegeService.listAll(null, ""));
    }

    @PreAuthorize("hasAuthority('notice')")
    @ApiOperation("查询学历")
    @GetMapping("/notice/xl")
    public JsonResult xl() {
        return JsonResult.ok().setData(dictService.cacheList(DictConstants.TYPE_XL));
    }

    @PreAuthorize("hasAuthority('notice')")
    @OperLog(value = "企业号推送", desc = "企业号推送", result = true)
    @ApiOperation("发送通知")
    @PostMapping("/notice/send")
    public JsonResult send(@RequestBody NoticeParams noticeParams) {
        return noticeService.send(noticeParams, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('notice')")
    @ApiOperation("导入学工号")
    @PostMapping("/notice/import")
    public JsonResult importExcel(MultipartFile file) {
        return noticeService.importExcel(file);
    }

    @PreAuthorize("hasAuthority('notice')")
    @ApiOperation(value = "导出")
    @GetMapping("/notice/export")
    public void export(Integer id, HttpServletResponse response) {
        noticeService.exportExcel(id, response);
    }

}
